package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommUser.COMM_USER;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommonActivityRegisterRecord.COMMON_ACTIVITY_REGISTER_RECORD;
import static com.tuanzi.loan.generated.jooq.loan.tables.ExternalPhoneRecord.EXTERNAL_PHONE_RECORD;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectConditionStep;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.family.custom.ExternalPhoneRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.ExternalShowSearchRequest;
import com.tuanzi.loan.vo.response.family.ExternalShowSearchResponse;

@SuppressWarnings({"rawtypes","unchecked"})
public class ExternalPhoneRecordRepositoryImpl extends JOOQBaseRepositoryPlus implements ExternalPhoneRecordRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return EXTERNAL_PHONE_RECORD;
	}
	
	//根据手机号码检查重复的记录
	@Override
	public List<String> repeatCheck(List<String> list) {
		List<String> resultList = dslContext.select(EXTERNAL_PHONE_RECORD.PHONE).from(EXTERNAL_PHONE_RECORD)
					.where(EXTERNAL_PHONE_RECORD.PHONE.in(list)).fetch(EXTERNAL_PHONE_RECORD.PHONE);
		return resultList;
	}

	
	@Override
	public List<String> findSource() {
		return (List<String>) dslContext.fetchValues("SELECT DISTINCT source from external_phone_record ");
	}
	
	@Override
	public Page<ExternalShowSearchResponse> list(
			ExternalShowSearchRequest searchRequest, Pageable pageable) {
		SelectConditionStep selectStep = dslContext.select(EXTERNAL_PHONE_RECORD.CREATE_TIME,
				EXTERNAL_PHONE_RECORD.SOURCE,EXTERNAL_PHONE_RECORD.PHONE).from(EXTERNAL_PHONE_RECORD)
				.whereNotExists(dslContext.select(DSL.field("1")).from(COMMON_ACTIVITY_REGISTER_RECORD)
				.where(EXTERNAL_PHONE_RECORD.PHONE.eq(COMMON_ACTIVITY_REGISTER_RECORD.PHONE)))
				.andNotExists(dslContext.select(DSL.field("1")).from(COMM_USER)
				.where(EXTERNAL_PHONE_RECORD.PHONE.eq(COMM_USER.MOBNUM)));
		
		Calendar cal = Calendar.getInstance();
		if(searchRequest.getEndTime()==null){
			selectStep.and(EXTERNAL_PHONE_RECORD.CREATE_TIME.le(new Timestamp(cal.getTimeInMillis())));
		}else{
			selectStep.and(EXTERNAL_PHONE_RECORD.CREATE_TIME.le(new Timestamp(searchRequest.getEndTime().getTime())));
		}
		
		cal.add(Calendar.DATE,-3); //前三天
		if(searchRequest.getStartTime()==null){
			selectStep.and(EXTERNAL_PHONE_RECORD.CREATE_TIME.ge(new Timestamp(cal.getTimeInMillis())));
		}else{
			selectStep.and(EXTERNAL_PHONE_RECORD.CREATE_TIME.ge(new Timestamp(searchRequest.getStartTime().getTime())));
		}
		
		if(!"".equals(searchRequest.getSource()) && searchRequest.getSource()!=null){
			selectStep=selectStep.and(EXTERNAL_PHONE_RECORD.SOURCE.eq(searchRequest.getSource()));
		}
		SelectQuery query = selectStep.orderBy(EXTERNAL_PHONE_RECORD.CREATE_TIME.desc()).getQuery();
		
		return search(query,pageable, new ExternalShowRecordMapper());
	}
	
	private final class ExternalShowRecordMapper implements RecordMapper<Record, ExternalShowSearchResponse> {

		@Override
		public ExternalShowSearchResponse map(Record record) {
			ExternalShowSearchResponse response=new ExternalShowSearchResponse();
			response.setImportTime(record.get(EXTERNAL_PHONE_RECORD.CREATE_TIME));
			response.setSource(record.get(EXTERNAL_PHONE_RECORD.SOURCE));
			response.setUserId(record.get(EXTERNAL_PHONE_RECORD.PHONE));
			return response;
		}
		
	}



}
